<?php
// *********** Global functions specialized for images/pictures ************

    // Resize image with preferred width and height:
     function getImageFromBlob($item_image, $width, $height){
        $image = imagecreatefromstring($item_image);

        ob_start();                 // Start output buffer:
        imagejpeg($image, null, 80);
        $data = ob_get_contents();
        ob_end_clean();             // End output buffer.

        if($width==0 && $height==0){  // If original size is preferred:
            return '<img src="data:image/jpg;base64,' .  base64_encode($data)  . '" />';
        }
        // return image in preferred size:
        return '<img src="data:image/jpg;base64,' .  base64_encode($data)  . '"'."
                width=$width height=$height />";
    }

    // Standard image size for small images:
    function getImageFromBlobResized($item_image){
        $image = imagecreatefromstring($item_image);

        ob_start();                 // Start output buffer:
        imagejpeg($image, null, 80);
        $data = ob_get_contents();
        ob_end_clean();             // End output buffer.

        // return image in standard size:
        return '<img src="data:image/jpg;base64,' .  base64_encode($data)  . '"
            width=195/>';
    }


    // Check the image type for accepted type(gif, jpeg, png) if the image is an image:
    function isImage($imageType){
        if (($imageType == "image/gif")  || ($imageType == "image/jpeg")
        || ($imageType == "image/pjpeg") || ($imageType == "image/png"))
            return true;
        return false;   // if the image is not an accepted image type.
    }

    // Create a temporary image to work on:
    function createTempImage($tmpName, $imageType){
        switch($imageType){
            case "image/gif":
                $image = imagecreatefromgif($tmpName);
                break;
            case "image/pjpeg":
                $image = imagecreatefromjpeg($tmpName);
                break;
            case "image/jpeg":
                $image = imagecreatefromjpeg($tmpName);
                break;
            case "image/png":
                $image = imagecreatefrompng($tmpName);
                break;
        }
        return $image;
    }

    // Get image data and resize image to proper size:
    function resizeImage($tmpName, $fileName, $imageType, $fileSize){
        if($fileSize>200000){   //If image is too high quality, lower the size:
            $MAXWIDTH = 250;
            $MAXHEIGHT = 250;
        }
        else{                   // Normal image quality:
            $MAXWIDTH = 330;
            $MAXHEIGHT = 330;
        }

        //Get the image original size
        list($width, $height) = getimagesize($tmpName);

        //Let the image get a new size
        $factor1 = $height/$MAXHEIGHT;
        $factor2 = $width/$MAXWIDTH;
        $factor = ($factor1>$factor2)?$factor1:$factor2;
        $new_width = $width / $factor;
        $new_height = $height / $factor;

        // Resample
        $image_p = imagecreatetruecolor($new_width, $new_height);
        $image = createTempImage($tmpName, $imageType);
        imagecopyresampled($image_p, $image,         // dest_im, src_im
                           0, 0, 0, 0,               // dst_x, dst_y, src_x, src_y
                           $new_width, $new_height,  // dst_w, dst_h
                           $width, $height);         // src_w, src_h

        imagejpeg($image_p,$fileName,100);

        // Destroy the tmp files:
        imagedestroy($image_p);
        imagedestroy($image);
        return $fileName;
    }

    // Get image content from filename:
    function getContent($fileName){
         // Get the image content:
        $fp      = fopen($fileName, 'r');
        $content = fread($fp, filesize($fileName));
        $content = addslashes($content);
        fclose($fp);
        return $content;
    }

   
?>
